package SG3D

import (
	. "SGEngine"
	"container/list"
	//"fmt"
	"github.com/gl"
)

var SGRenderQueue *RenderQueue

type RenderQueue struct {
	TransparentRenderList *Queue
	RenderList            *Queue
}

func NewRenderQueue() *RenderQueue {
	renderqueue := new(RenderQueue)
	renderqueue.RenderList = NewQueue()
	renderqueue.TransparentRenderList = NewQueue()
	return renderqueue
}

func (r *RenderQueue) AddToQueue(node INode) {
	SGRenderQueue.RenderList.Push(node)
}

func (r *RenderQueue) Render() {
	r.RenderList.Each(func(e *list.Element) {
		if r := e.Value.(INode).GetRender(); r != nil {
			gl.PushMatrix()
			e.Value.(INode).GetTransfrom().TransfromNode()
			r.Render()
			e.Value.(INode).GetTransfrom().EndTransfromNode()
			gl.PopMatrix()
		}
	})

	r.TransparentRenderList.Each(func(e *list.Element) {
		if r := e.Value.(INode).GetRender(); r != nil {
			r.Render()
		}
	})
}

func (r *RenderQueue) Clear() {
	r.RenderList.Clear()
	r.TransparentRenderList.Clear()
}
